﻿Imports OMANE.Common
Imports OMANE.Common.UI
Imports System.Collections.ObjectModel

''' <summary>
''' 
''' </summary>
''' <remarks></remarks>
<ModuleInfo()> _
Public Class BuilderModule
    Implements IModule


    Dim onglets As V2ViewModelTabGroup
    Dim noeuds As V2ViewModelTabGroup
    Dim donneesExternes As V2ViewModelTabGroup
    Dim modeleInterne As V2ViewModelTabGroup
    Dim parametres As V2ViewModelTabGroup
    Dim calculs As V2ViewModelTabGroup
    Dim graphs As V2ViewModelTabGroup
    Dim tableaux As V2ViewModelTabGroup
    Dim _ViewModelParametres As IViewModelTabSection
    Dim modeleInterneActions As V2ViewModelTabGroup
    Dim _ViewModelModeleInterne As ViewModelModeleInterne
    Dim TestLocal As V2ViewModelTabGroup

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public ReadOnly Property Dependencies As System.Collections.Generic.IEnumerable(Of Common.ModuleReference) Implements Common.IModule.Dependencies
        Get

        End Get
    End Property

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Load() Implements Common.IModule.Load
        'Dim wt = New WorkTable()
        'Dim col = New WorkTable.Column(wt)
        'col.Index = 0


        'Dim o2 = col.Table.Compute(0, col + 1)

        'Dim X = New LocalParamTable("X")
        'Dim Y = New LocalParamTable("Y")

        'Dim o = (X - 1) / (Y - 1)

        'Dim p = Y ^ 2 / (X ^ 2 + 1)

        'Dim q = ((X + 1) > (Y - 1)) And ((X - 1) < (Y + 1))

        'Dim r = LocalExpression.Choose(X > Y, X / Y, Y / X)

        'Dim data = Enumerable.Range(0, 10).AsQueryable()

        'Dim f = Expressions.Expression.Lambda(r.Expr, r.Params).Compile()


        'Dim o1 = 1

        Dim rv1 = New RandomValue("rv1")
        Dim rv2 = New RandomValue("rv2")
        Dim rv3 = New RandomValue("rv3")

        Dim n = rv3 * (rv1 > rv2)

        Dim nv = n.Compute(1)

        Dim o = (rv1 + rv2) / (rv1 - rv2)

        Dim p = (rv3 >= rv1) And (rv3 <= rv2)

        Dim ol = New RandomValue(rv1 * rv1 / (1 + rv1 * rv1))

        Dim kol = rv2 * ol + (1 - ol) * rv3


        Dim q = RandomValue.Choose(rv1 > rv2, rv2 * rv3, rv1 * rv3)

        Dim r = RandomValue.Min(rv1, rv2, rv3)

        Dim s = RandomValue.Log( _
                 RandomValue.Choose( _
                 rv1 + rv2 + rv3, _
                 RandomValue.Log(rv1), RandomValue.Log(rv2), RandomValue.Log(rv3)))

        Dim t = rv1 / (1 + rv1 * rv1)

        Dim u = (rv1 > -1) And (rv1 < 1)

        Dim v = rv2 * u + rv3


        Dim str = s.ToString()
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Reset(ByVal Application As Common.Application) Implements Common.IModule.Reset

        'Dim q = From i In Enumerable.Range(0, 5) Select New Data.DataColumn("colonne_" & i, GetType(String))

        'Dim t = TypeBuilder.FromColumns(q.ToArray())

        'Return

        Dim ui = Application.GetService(Of IUIService)()
        Dim dicTemplate = New Windows.ResourceDictionary()
        dicTemplate.Source = New Uri("/OMANE.Modules.Builder;component/RessourcesTemplate.xaml", System.UriKind.Relative)
        ui.AddResourceDictionary(dicTemplate)

        Dim acc As V2ViewModelAccordeonMenuItem = New V2ViewModelAccordeonMenuItem()
        With acc
            .Header = "Module Builder"
            .Icone = Theme.StandardIcons.HOUSE
            .Body = New UI.V2ViewModelTrvMenu()
        End With

        Dim root As V2ViewModelTabGroup = New V2ViewModelTabGroup()
        With root
            .Icone = Theme.StandardIcons.HOUSE
            .Title = "Builder"
        End With

        acc.Body.Add(root)

        ui.V2ViewModelAccordeonMenu.Add(acc)

        buildModelNode(acc.Body.Root)
        buildVisualNode(acc.Body.Root)
        buildOptionNode(acc.Body.Root)

        ui.ViewModelMainMenu.File.Children.Add( _
            New ViewModelMainMenuItem() With {.Title = "Sauvegarder le projet de module", _
               .Command = New DelegateCommand(Of Object)(AddressOf SaveProjectExecute)})
        ui.ViewModelMainMenu.File.Children.Add( _
          New ViewModelMainMenuItem() With {.Title = "Charger un  projet de module", _
             .Command = New DelegateCommand(Of Object)(AddressOf LoadProjectExecute)})

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Unload() Implements Common.IModule.Unload

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub buildModelNode(ByVal iViewModelTrvMenuItem As IViewModelTrvMenuItem)
        Dim root As V2ViewModelTabGroup = New V2ViewModelTabGroup()
        iViewModelTrvMenuItem.Children.Add(root)
        With root
            .Icone = Theme.StandardIcons.HOUSE
            .Title = "Modele Fonctionnel"
        End With

        donneesExternes = New V2ViewModelTabGroup() With {.Title = "Donnees Externes"}

        modeleInterne = New V2ViewModelTabGroup() With {.Title = "Modele Interne"}

        parametres = New V2ViewModelTabGroup() With {.Title = "Parametres"}

        calculs = New V2ViewModelTabGroup() With { _
            .Title = "Calculs", _
            .TabGroup = New ObservableCollection(Of IViewModelTabSection)}



        root.Children.Add(donneesExternes)
        root.Children.Add(modeleInterne)
        root.Children.Add(parametres)
        root.Children.Add(calculs)

        Dim rootgroup = New ObservableCollection(Of IViewModelTabSection)
        root.TabGroup = rootgroup
        Me._ViewModelParametres = New ViewModelParametres()
        Me._ViewModelModeleInterne = New ViewModelModeleInterne()
        rootgroup.Add(Me._ViewModelParametres)
        rootgroup.Add(Me._ViewModelModeleInterne)

        parametres.TabSection = rootgroup(0)
        parametres.TabGroup = rootgroup

        modeleInterne.TabSection = rootgroup(1)
        modeleInterne.TabGroup = rootgroup

        modeleInterneActions = New V2ViewModelTabGroup() With {.Title = "Actions", .TabGroup = New ObservableCollection(Of IViewModelTabSection)()}
        modeleInterneActions.ContextualCommands.Add(
            New ViewModelCommand("Ajouter une action", New DelegateCommand(Of Object)(AddressOf AddActionExecute)))
        modeleInterne.Children.Add(modeleInterneActions)

        Dim ajoutertraitemementexecute = Sub(p As Object)
                                             Dim tname = InputBox("Nom du Traitememnt")
                                             Dim t = New ViewModelTraitement(Me._ViewModelModeleInterne, Me._ViewModelParametres) _
                                                     With {.Title = If(String.IsNullOrEmpty(tname), "UnNamed", tname)}
                                             calculs.TabGroup.Add(t)
                                             If calculs.TabGroup.Count = 1 Then
                                                 calculs.TabSection = t
                                             End If
                                             Dim c = New V2ViewModelTabGroup() _
                                                     With { _
                                                      .Title = If(String.IsNullOrEmpty(tname), "UnNamed", tname), _
                                                      .TabGroup = calculs.TabGroup, _
                                                      .TabSection = t}
                                             calculs.Children.Add(c)
                                         End Sub
        calculs.ContextualCommands.Add( _
            New ViewModelCommand("Ajouter un Traitement", _
            New DelegateCommand(Of Object)(ajoutertraitemementexecute)))




    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub buildVisualNode(ByVal iViewModelTrvMenuItem As IViewModelTrvMenuItem)
        Dim root As V2ViewModelTabGroup = New V2ViewModelTabGroup()
        iViewModelTrvMenuItem.Children.Add(root)
        With root
            .Icone = Theme.StandardIcons.HOUSE
            .Title = "Modele Visuel"
        End With

        onglets = New V2ViewModelTabGroup() With {.Title = "Onglets", .TabGroup = New ObservableCollection(Of IViewModelTabSection)}
        noeuds = New V2ViewModelTabGroup() With {.Title = "Noeuds de Navigations"}
        graphs = New V2ViewModelTabGroup() With {.Title = "Graphiques"}
        tableaux = New V2ViewModelTabGroup() With {.Title = "Tableaux"}

        root.ContextualCommands.Add( _
            New ViewModelCommand("Ajouter un onglet statique", New DelegateCommand(Of Object)(AddressOf AddStaticTab)))
        root.ContextualCommands.Add( _
            New ViewModelCommand("Ajouter un onglet dynamiqre", New DelegateCommand(Of Object)(AddressOf AddDynamicTab)))
        root.ContextualCommands.Add( _
            New ViewModelCommand("Ajouter un noeud", New DelegateCommand(Of Object)(AddressOf AddNode)))



        root.Children.Add(onglets)
        root.Children.Add(noeuds)
        root.Children.Add(graphs)
        root.Children.Add(tableaux)

        TestLocal = New V2ViewModelTabGroup() With {.Title = "Test", .TabGroup = New ObservableCollection(Of IViewModelTabSection)}
        TestLocal.TabGroup.Add(New VMTester(modeleInterne.TabSection, parametres.TabSection, True))
        TestLocal.TabSection = TestLocal.TabGroup(0)
        root.Children.Add(TestLocal)
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub buildOptionNode(ByVal iViewModelTrvMenuItem As IViewModelTrvMenuItem)
        Dim root As V2ViewModelTabGroup = New V2ViewModelTabGroup()
        iViewModelTrvMenuItem.Children.Add(root)
        With root
            .Icone = Theme.StandardIcons.HOUSE
            .Title = "Options"
        End With
    End Sub

    Private Sub ExecuteCustom(ByVal objet As Object)
        'Throw New NotImplementedException
    End Sub

    Private Sub AddStaticTab(ByVal objet As Object)
        Dim tname = InputBox("Nom de l'onglet")
        tname = If(String.IsNullOrEmpty(tname), "UnNamed", tname)
        Dim ong = New ViewModelEditableParameterSection(Me._ViewModelParametres) With {.Title = tname}
        onglets.TabGroup.Add(ong)
        Dim tab As V2ViewModelTabGroup = New V2ViewModelTabGroup() With {.Title = ong.Title, .TabSection = ong, .TabGroup = onglets.TabGroup}
        onglets.Children.Add(tab)
        tab.ContextualCommands.Add(
            New ViewModelCommand("Ajouter un Groupe", ong.AddGroupCommand))


    End Sub

    Private Sub AddDynamicTab(ByVal objet As Object)

        onglets.Children.Add(New V2ViewModelTabGroup() With {.Title = "Section Dynamique"})

    End Sub

    Private Sub AddNode(ByVal objet As Object)
        noeuds.Children.Add(New V2ViewModelTabGroup() With {.Title = "Onglets"})

    End Sub

    Private UpdateDates As New List(Of DateTime)

    Private Sub SaveProjectExecute(ByVal objet As Object)
        Dim proj = <Project CreationDate=<%= Now %> Creator=<%= My.User.Name %>/>


        Dim projupdate = <Update CreationDate=<%= Now %> Creator=<%= My.User.Name %>/>
        proj.Add(projupdate)

        Dim temp As ViewModelModeleInterne = modeleInterne.TabSection
        projupdate.Add(temp.Model.GetXml())

        Dim temp2 As ViewModelParametres = parametres.TabSection
        Dim paramxml = <Parameters><%= From p In temp2.Params Select p.GetXml() %></Parameters>

        Dim savdg = New Microsoft.Win32.SaveFileDialog()
        If savdg.ShowDialog() Then
            proj.Save(savdg.FileName)
        End If
    End Sub

    Private Sub LoadProjectExecute(ByVal objet As Object)

    End Sub

    Private Sub AddActionExecute(ByVal objet As Object)

    End Sub
End Class

